package com.flower.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.flower.pojo.Product;
import com.flower.service.ISearchItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 搜索模块
 */
@Controller
public class SearchItemController {
	
	@Resource(name = "searchItemService")
	private ISearchItemService iSearchItemService;
	
	@RequestMapping("/selectAllProductByPname.do")
	public String showResultByPname(@RequestParam(required=true, defaultValue="1") Integer startPage, 
			HttpServletRequest request, Model model) throws UnsupportedEncodingException{
		String key = request.getParameter("key");
		//解决url地址传输时，出现的乱码情况
		key = URLDecoder.decode(key, "UTF-8");
		model.addAttribute("KEY", key);
		
		
		PageHelper.startPage(startPage, 12);
		List<Product> products = iSearchItemService.selectAllProductByPname(key);
		PageInfo<Product> pi = new PageInfo<>(products);
		model.addAttribute("PAGE", pi);
		model.addAttribute("PRODUCTS", products);
		
		return "search";
	}
	
}
